package com.example.xiaomi.Model.Service;

import com.example.xiaomi.Model.Controller.DaoList;
import com.example.xiaomi.Model.Controller.GodsList;
import com.example.xiaomi.Model.Controller.adddetailsList;
import com.example.xiaomi.Model.entity.GodsEntity;
import com.example.xiaomi.Model.entity.UeserEntity;
import com.example.xiaomi.Model.entity.adddetailsEntity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/Front/page")
public class Front_page extends HttpServlet {
    DaoList dao = new DaoList();
    GodsList godsList = new GodsList();
    adddetailsList adddetailsList = new adddetailsList();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if (id != null){
            String[] idarr = id.split(",");
            List<UeserEntity> inquire_All = dao.selectAll();
            for (UeserEntity list: inquire_All) {
                if (idarr[0].contains("@qq.com")){
                    if (idarr[0].equals(list.getUpostbox())){

                        req.setAttribute("uname",list.getUname());
                    }
                }else{
                    int id_Int = Integer.parseInt(idarr[1]);
                    if (id_Int == list.getUid()){
                        req.setAttribute("uname",list.getUname());
                    }
                }
            }
        }

        List<GodsEntity> list = godsList.selectall();
        String[][] arr = new String[list.size()][6];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {

                if (j == 0){
                    arr[i][j] = list.get(i).getGname();
                }else if (j == 1){
                    arr[i][j] = String.valueOf(list.get(i).getGprice());
                }else if (j == 2){
                    arr[i][j] = list.get(i).getPing();
                }else if (j == 3){
                    arr[i][j] = list.get(i).getIs_sort();
                }else if (j == 4){
                    arr[i][j] = list.get(i).getSname();
                }else if (j == 5){
                    arr[i][j] = String.valueOf(list.get(i).getSid());
                }
            }
        }
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < arr.length; i++ ){
            for(int j = 0; j < arr[i].length; j++){
                sb.append(","+arr[i][j]);
            }
            req.setAttribute("commodity"+i,sb.toString());
            sb.setLength(0);
        }
        //长度
        req.setAttribute("size",arr.length);

        req.getRequestDispatcher("/WEB-INF/Xiaomi/Front_page.jsp").forward(req,resp);
    }
}
